/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Ron
 */
public class FBanqueJpaController implements Serializable {

    public FBanqueJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;
    
    public FBanqueJpaController() {
        this.emf = Persistence.createEntityManagerFactory("projetT_RPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FBanque FBanque) throws PreexistingEntityException, Exception {
        if (FBanque.getFEbanqueCollection() == null) {
            FBanque.setFEbanqueCollection(new ArrayList<FEbanque>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<FEbanque> attachedFEbanqueCollection = new ArrayList<FEbanque>();
            for (FEbanque FEbanqueCollectionFEbanqueToAttach : FBanque.getFEbanqueCollection()) {
                FEbanqueCollectionFEbanqueToAttach = em.getReference(FEbanqueCollectionFEbanqueToAttach.getClass(), FEbanqueCollectionFEbanqueToAttach.getCbMarq());
                attachedFEbanqueCollection.add(FEbanqueCollectionFEbanqueToAttach);
            }
            FBanque.setFEbanqueCollection(attachedFEbanqueCollection);
            em.persist(FBanque);
            for (FEbanque FEbanqueCollectionFEbanque : FBanque.getFEbanqueCollection()) {
                FBanque oldBQNoOfFEbanqueCollectionFEbanque = FEbanqueCollectionFEbanque.getBQNo();
                FEbanqueCollectionFEbanque.setBQNo(FBanque);
                FEbanqueCollectionFEbanque = em.merge(FEbanqueCollectionFEbanque);
                if (oldBQNoOfFEbanqueCollectionFEbanque != null) {
                    oldBQNoOfFEbanqueCollectionFEbanque.getFEbanqueCollection().remove(FEbanqueCollectionFEbanque);
                    oldBQNoOfFEbanqueCollectionFEbanque = em.merge(oldBQNoOfFEbanqueCollectionFEbanque);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findFBanque(FBanque.getCbMarq()) != null) {
                throw new PreexistingEntityException("FBanque " + FBanque + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FBanque FBanque) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FBanque persistentFBanque = em.find(FBanque.class, FBanque.getCbMarq());
            Collection<FEbanque> FEbanqueCollectionOld = persistentFBanque.getFEbanqueCollection();
            Collection<FEbanque> FEbanqueCollectionNew = FBanque.getFEbanqueCollection();
            List<String> illegalOrphanMessages = null;
            for (FEbanque FEbanqueCollectionOldFEbanque : FEbanqueCollectionOld) {
                if (!FEbanqueCollectionNew.contains(FEbanqueCollectionOldFEbanque)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain FEbanque " + FEbanqueCollectionOldFEbanque + " since its BQNo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<FEbanque> attachedFEbanqueCollectionNew = new ArrayList<FEbanque>();
            for (FEbanque FEbanqueCollectionNewFEbanqueToAttach : FEbanqueCollectionNew) {
                FEbanqueCollectionNewFEbanqueToAttach = em.getReference(FEbanqueCollectionNewFEbanqueToAttach.getClass(), FEbanqueCollectionNewFEbanqueToAttach.getCbMarq());
                attachedFEbanqueCollectionNew.add(FEbanqueCollectionNewFEbanqueToAttach);
            }
            FEbanqueCollectionNew = attachedFEbanqueCollectionNew;
            FBanque.setFEbanqueCollection(FEbanqueCollectionNew);
            FBanque = em.merge(FBanque);
            for (FEbanque FEbanqueCollectionNewFEbanque : FEbanqueCollectionNew) {
                if (!FEbanqueCollectionOld.contains(FEbanqueCollectionNewFEbanque)) {
                    FBanque oldBQNoOfFEbanqueCollectionNewFEbanque = FEbanqueCollectionNewFEbanque.getBQNo();
                    FEbanqueCollectionNewFEbanque.setBQNo(FBanque);
                    FEbanqueCollectionNewFEbanque = em.merge(FEbanqueCollectionNewFEbanque);
                    if (oldBQNoOfFEbanqueCollectionNewFEbanque != null && !oldBQNoOfFEbanqueCollectionNewFEbanque.equals(FBanque)) {
                        oldBQNoOfFEbanqueCollectionNewFEbanque.getFEbanqueCollection().remove(FEbanqueCollectionNewFEbanque);
                        oldBQNoOfFEbanqueCollectionNewFEbanque = em.merge(oldBQNoOfFEbanqueCollectionNewFEbanque);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = FBanque.getCbMarq();
                if (findFBanque(id) == null) {
                    throw new NonexistentEntityException("The fBanque with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FBanque FBanque;
            try {
                FBanque = em.getReference(FBanque.class, id);
                FBanque.getCbMarq();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The FBanque with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<FEbanque> FEbanqueCollectionOrphanCheck = FBanque.getFEbanqueCollection();
            for (FEbanque FEbanqueCollectionOrphanCheckFEbanque : FEbanqueCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This FBanque (" + FBanque + ") cannot be destroyed since the FEbanque " + FEbanqueCollectionOrphanCheckFEbanque + " in its FEbanqueCollection field has a non-nullable BQNo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(FBanque);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FBanque> findFBanqueEntities() {
        return findFBanqueEntities(true, -1, -1);
    }

    public List<FBanque> findFBanqueEntities(int maxResults, int firstResult) {
        return findFBanqueEntities(false, maxResults, firstResult);
    }

    private List<FBanque> findFBanqueEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(FBanque.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FBanque findFBanque(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FBanque.class, id);
        } finally {
            em.close();
        }
    }

    public int getFBanqueCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<FBanque> rt = cq.from(FBanque.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
